package com.treeman.controller;

import java.util.Locale;

import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.treeman.user.UserRole.Role;

@Controller
@RequestMapping(value = "/")
public class BaseController {

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView startApplication(Locale locale) {

		ModelAndView mav = new ModelAndView("core/app");
		SecurityContext context = SecurityContextHolder.getContext();

		if (context.getAuthentication().getAuthorities()
				.contains(new GrantedAuthorityImpl(Role.ROLE_USER.toString()))) {
			mav.addObject("isLogged", true);
			mav.addObject("username", context.getAuthentication().getName());
		} else {
			mav.addObject("isLogged", false);
		}
		mav.addObject("locale", locale.getLanguage());

		return mav;
	}
}
